package com.sun.springCache;

import java.io.IOException;

import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.core.io.ClassPathResource;

import net.sf.ehcache.CacheException;

public class SpringEHCacheMain {

	/**
	 * 测试缓存 一旦被缓存 就不会再次获取值了
	 * 
	 * @throws IOException
	 * @throws CacheException
	 */
	@Test
	public void test1() throws Exception {

		// 创建底层Cache
		net.sf.ehcache.CacheManager ehcacheManager = new net.sf.ehcache.CacheManager(new ClassPathResource("spring/ehcache.xml").getInputStream());

		// 创建Spring的CacheManager
		EhCacheCacheManager cacheCacheManager = new EhCacheCacheManager();
		// 设置底层的CacheManager
		cacheCacheManager.setCacheManager(ehcacheManager);
		Cache cache = cacheCacheManager.getCache("myCache");
		Person p = new Person("1", "name1", "group1");
		cache.put(p.getId(), p);
		System.out.print(cache.get(p.getId()));
	}
}
